<template>
  <el-card>
    <!-- 查询 -->
    <el-form :model="queryParams" inline label-width="80px" class="search-form">
<!--      <el-form-item label="病房编号">-->
<!--        <el-input v-model="queryParams.wardNo" clearable placeholder="请输入病房编号" style="width: 150px" />-->
<!--      </el-form-item>-->

      <el-form-item label="所属科室">
        <el-select v-model="queryParams.roomName" clearable placeholder="请选择科室" style="width: 150px">
          <el-option label="全部" value="" />
          <el-option v-for="r in allRoomOpts" :key="r" :label="r" :value="r" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="success" @click="handleAdd">新增病房</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" border stripe style="width: 100%; margin:12px 0">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="wardNo" label="病房编号" />
      <el-table-column prop="roomName" label="所属科室" />
      <el-table-column prop="bedCount" label="床位数" width="70" />
      <el-table-column label="操作" width="220">
        <template #default="{row}">
          <el-button size="small" @click="handleBed(row)">查看病床</el-button>
          <el-button size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDel(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        :page-size="pagination.pageSize"
        :current-page="pagination.pageNum"
        :page-sizes="[5,10,20,50]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </el-card>

  <!-- 新增/编辑弹窗 -->
  <el-dialog v-model="dialogVisible" :title="dialogTitle" width="480px">
    <el-form ref="formRef" :model="form" label-width="100px" :rules="formRules">
      <el-form-item label="病房编号" prop="wardNo">
        <el-input v-model="form.wardNo" placeholder="请输入病房编号" />
      </el-form-item>
      <el-form-item label="所属科室" prop="medicalRoomId">
        <el-select v-model="form.medicalRoomId" placeholder="请选择科室" style="width:100%">
          <el-option v-for="r in roomOpts" :key="r.id" :label="r.roomName" :value="r.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="床位数" prop="bedCount">
        <el-input-number v-model="form.bedCount" :min="0" controls-position="right" style="width:100%" />
      </el-form-item>
      <!-- 【费用】字段已删除 -->
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible=false">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { ElMessage, ElMessageBox } from 'element-plus'
import { findWard, listWard, addWard, updateWard, delWard, roomList } from '../api/ward.js'

export default {
  name: 'WardViewNoPrice',
  data() {
    return {
      queryParams: { wardNo: '', roomName: '' }, // 无 price
      pagination: { pageNum: 1, pageSize: 10, total: 0 },
      tableData: [],
      dialogVisible: false,
      dialogTitle: '',
      isAdd: true,
      form: {},
      allRoomOpts: [],
      roomOpts: []
    }
  },
  computed: {
    formRules() {
      return {
        wardNo: [{required: true, message: '请输入病房编号', trigger: 'blur'}],
        medicalRoomId: [{required: true, message: '请选择科室', trigger: 'change'}],
        bedCount: [{required: true, message: '请输入床位数', trigger: 'blur'}]
        // price 校验已移除
      }
    }
  },
  mounted() {
    this.loadData()
    this.loadRoom()
  },
  methods: {
    async loadData() {
      const {pageNum, pageSize} = this.pagination
      const hasCondition = this.queryParams.wardNo || this.queryParams.roomName
      const api = hasCondition ? listWard : findWard
      const params = hasCondition
          ? {...this.queryParams, pageNum, pageSize}
          : {pageNum, pageSize}
      const {data: res} = await api(params)
      if (res.code === 200) {
        this.tableData = res.obj.list || []
        this.pagination.total = res.obj.total
      } else {
        ElMessage.error(res.msg || '加载失败')
      }
    },
    async loadRoom() {
      const {data: res} = await roomList()
      if (res.code === 200) {
        this.roomOpts = res.obj || []
        this.allRoomOpts = (res.obj || []).map(r => r.roomName)
      }
    },
    handleQuery() {
      this.pagination.pageNum = 1
      this.loadData()
    },
    handleReset() {
      this.queryParams = {wardNo: '', roomName: ''}
      this.handleQuery()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.loadData()
    },
    handleCurrentChange(val) {
      this.pagination.pageNum = val
      this.loadData()
    },
    handleAdd() {
      this.isAdd = true
      this.dialogTitle = '新增病房'
      this.form = {wardNo: '', medicalRoomId: '', bedCount: 0}
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.isAdd = false
      this.dialogTitle = '编辑病房'
      this.form = {...row}
      this.dialogVisible = true
    },
    handleBed(row) {
      this.$router.push({path: '/bed', query: {wardId: row.id}})
    },
    async submitForm() {
      await this.$refs.formRef.validate()
      const api = this.isAdd ? addWard : updateWard
      const {data: res} = await api(this.form)
      if (res.code === 200) {
        ElMessage.success(this.isAdd ? '新增成功' : '修改成功')
        this.dialogVisible = false
        this.loadData()
      } else {
        ElMessage.error(res.msg || '操作失败')
      }
    },
    handleDel(row) {
      ElMessageBox.confirm('确认删除该病房？', '提示', {type: 'warning'})
          .then(async () => {
            const {data: res} = await delWard(row.id)
            if (res.code === 200) {
              ElMessage.success('删除成功')
              this.loadData()
            } else {
              ElMessage.error(res.msg || '删除失败')
            }
          })
          .catch(() => {
          })
    }
  }
}
</script>

<style scoped>
.search-form {
  margin-bottom: 12px;
}
</style>